package aar.test.tessocr.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import aar.test.tessocr.adapter.interfaces.BaseAdapter;
import aar.test.tessocr.model.UnexporderListModel;
import clx.tessocr.ocreng.R;

/**
 * Created by sunshine on 2020/7/27
 */
public class OrderListAdapter extends BaseAdapter<OrderListAdapter.OrderListItemHolder> {
    private List<UnexporderListModel> mList;
    private Context context;



    public OrderListAdapter(Context mContext, @NonNull List<UnexporderListModel> mList) {
        this.mList = mList;
        this.context = mContext;
    }

    @Override
    public OrderListAdapter.OrderListItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.exporder_list_item, parent, false);
        OrderListAdapter.OrderListItemHolder holder = new OrderListAdapter.OrderListItemHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull OrderListAdapter.OrderListItemHolder holder, final int position) {
        holder.order_id.setText(mList.get(position).getExpress_order());


        holder.order_phone.setText(mList.get(position).getPhone());


        holder.cabinet_name.setText(mList.get(position).getCabinetName());

        //holder.grid_type.setText(mList.get(position).getGridType());


        holder.grid_address.setText(mList.get(position).getGridNum()+"格");

        holder.cabinet_address.setText(mList.get(position).getCabinetAddress());

        holder.cabinet_statues.setText(mList.get(position).getStatus());

        holder.cabinet_time.setText("存件时间:" + mList.get(position).getCreated_at());

        if (!TextUtils.equals(mList.get(position).getStatus(),"已取件")) {
            holder.picked_time.setVisibility(View.GONE);
        } else {

            if (!TextUtils.isEmpty(mList.get(position).getGetTime())) {
                holder.picked_time.setVisibility(View.VISIBLE);
                holder.picked_time.setText("取件时间:" + mList.get(position).getGetTime());
            } else {
                holder.picked_time.setVisibility(View.GONE);
            }
        }


        if(TextUtils.equals(mList.get(position).getStatus(),"已取件")){
            holder.ll_button.setVisibility(View.GONE);
            //holder.order_grid_open.setVisibility(View.GONE);
            //holder.order_grid_quhui.setVisibility(View.GONE);
            //holder.order_play_number_phone.setVisibility(View.GONE);

        }else if(TextUtils.equals(mList.get(position).getStatus(),"未取件")){
            holder.ll_button.setVisibility(View.VISIBLE);
            holder.order_grid_open.setVisibility(View.VISIBLE);
            holder.order_grid_open.setText(context.getResources().getString(R.string.modify_phone));
            holder.order_grid_quhui.setVisibility(View.VISIBLE);
            holder.order_play_number_phone.setVisibility(View.VISIBLE);

        } else if(TextUtils.equals(mList.get(position).getStatus(),"5")){
            holder.ll_button.setVisibility(View.GONE);
            //holder.order_grid_open.setVisibility(View.GONE);
            //holder.order_grid_quhui.setVisibility(View.GONE);
            //holder.order_play_number_phone.setVisibility(View.GONE);
            holder.ll_cabinet_address.setVisibility(View.GONE);
            holder.ll_cabinet_name.setVisibility(View.GONE);
            holder.cabinet_statues.setText("已录入");
        } else {
            holder.ll_button.setVisibility(View.VISIBLE);
            holder.order_grid_open.setVisibility(View.VISIBLE);
            holder.order_grid_quhui.setVisibility(View.VISIBLE);
            holder.order_grid_quhui.setText(context.getResources().getString(R.string.post_again));
            holder.order_play_number_phone.setVisibility(View.VISIBLE);
        }

        holder.order_grid_quhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(null!=mViewClickListener){
                    if (TextUtils.equals(mList.get(position).getStatus(),"未取件")) {
                        mViewClickListener.gridTackBack(position);
                    } else {
                        mViewClickListener.post_again(position);
                    }

                }
            }
        });

        holder.order_grid_open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(null!=mViewClickListener){
                    if (TextUtils.equals(mList.get(position).getStatus(),"未取件")) {
                        mViewClickListener.modify_phone(position);
                    } else {
                        mViewClickListener.gridTackBack(position);
                    }
                }
            }
        });

        holder.order_play_number_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(!TextUtils.isEmpty(mList.get(position).getPhone())){


                    Intent intent=new Intent();
                    intent.setAction(Intent.ACTION_CALL);
                    intent.setData(Uri.parse("tel:" + mList.get(position).getPhone()));
                    //开启系统拨号器
                    context.startActivity(intent);


                }else{
                    Toast.makeText(context, "电话号码为空", Toast.LENGTH_SHORT).show();


                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class OrderListItemHolder extends RecyclerView.ViewHolder {
        TextView order_id;
        TextView order_phone;
        TextView cabinet_name;
        TextView grid_type;
        TextView grid_address;
        TextView cabinet_address;
        TextView cabinet_statues;
        TextView cabinet_time;
        TextView picked_time;
        TextView order_grid_open;
        TextView order_grid_quhui;
        TextView order_play_number_phone;
        LinearLayout ll_cabinet_address;
        LinearLayout ll_cabinet_name;
        LinearLayout ll_button;

        public OrderListItemHolder(View itemView) {
            super(itemView);
            order_id=itemView.findViewById(R.id.order_id);
            order_phone=itemView.findViewById(R.id.order_phone);
            cabinet_name=itemView.findViewById(R.id.cabinet_name);
            picked_time=itemView.findViewById(R.id.picked_time);
            grid_type=itemView.findViewById(R.id.grid_type);
            grid_address=itemView.findViewById(R.id.grid_address);
            cabinet_address=itemView.findViewById(R.id.cabinet_address);
            cabinet_statues=itemView.findViewById(R.id.cabinet_statues);
            cabinet_time=itemView.findViewById(R.id.cabinet_time);
            order_grid_open=itemView.findViewById(R.id.order_grid_open);
            order_grid_quhui=itemView.findViewById(R.id.order_grid_quhui);
            order_play_number_phone=itemView.findViewById(R.id.order_play_number_phone);
            ll_cabinet_name=itemView.findViewById(R.id.ll_cabinet_name);
            ll_cabinet_address=itemView.findViewById(R.id.ll_cabinet_address);
            ll_button=itemView.findViewById(R.id.ll_button);
        }
    }

    public interface ViewClickListener{
        void post_again(int pos);
        void gridTackBack(int pos);
        void modify_phone(int pos);
    }

    public void setViewClickListener(OrderListAdapter.ViewClickListener mViewClickListener){
        this.mViewClickListener=mViewClickListener;

    }


    private OrderListAdapter.ViewClickListener mViewClickListener;
}